<?php
require_once 'model.php';
require_once 'controller.php';
require_once 'offer_model.php';
require_once 'beer_model.php';
require_once 'venue_model.php';
require_once 'melbourne.php';
/**
 * Controls all the operations related to Offers
 * @author The hungry Guys
 *
 */
class offer_controller extends controller{
	public $offer;
	public $request;

	/**
	 * Constructor
	 * @return void
	 */
	function __construct(){
		//load the model
		$this->offer = new offer_model();
	}
	
	/**
	 * Function to retrieve a specific offer
	 * @param $args page (offers ID)
	 * @return void
	 */
	function get(){
		$view->offers = array();
		if(isset($this->request['id'])){
			$view->offers[] = $this->offer->get($this->request['id']);
		}
		//render the view
		include('offer_view.php');
	}

/**
 * Function to Create a new Offer
 * All arguments are taken from the HTTP Request
 */
function create(){
		$this->requireLogin();
		if ($_SERVER['REQUEST_METHOD'] == 'POST'){

	  	$repeats = "";
	  	if(isset($this->request['mon'])){
	  		$repeats=$repeats."1";
	  	}
	  	if(isset($this->request['tue'])){
	  		$repeats=$repeats."2";
	  	}
	  	if(isset($this->request['wed'])){
	  		$repeats=$repeats."3";
	  	}
	  	if(isset($this->request['thu'])){
	  		$repeats=$repeats."4";
	  	}
	  	if(isset($this->request['fri'])){
	  		$repeats=$repeats."5";
	  	}
	  	if(isset($this->request['sat'])){
	  		$repeats=$repeats."6";
	  	}
	  	if(isset($this->request['sun'])){
	  		$repeats=$repeats."7";
	  	}
	  	$this->request['repeats']=$repeats;
	  	$this->request['user_id']=$_SESSION['user']['id'];
	  	$result = $this->offer->create($this->request);
		  	if ($result==true){
				$this->setFlash("Your offer has been Created");
				
		  	}else{
		  		$this->setFlash("Ups! Something Happened");
		  	}
	  	redirect(HOME_PAGE);
		}
		else{
			$beer = new beer_model();
			$venue = new venue_model();
			$view->beers = $beer->getAll2($this->request);
			$sizes = new model("size");
			$view->sizes = $sizes->getAll();

			$view->venues = $venue->getAll();

			include('offer_create_view.php');
		}
	}

	/**
	 * Default action to perform when no action is called
	 * @param $args page
	 * @return void
	 */
	function index(){
		$this->find();

	}
	
	/**
	 * Finds (Searches) Offers according to parameters passed in HTTP Request
	 */
	function find(){
//		$offers=new offer_model();
//		print_r($this->request);exit;
		if (isset($this->request['suburb']) || isset($this->request['beer_name']) || isset($this->request['price'])){
		$view->request = $this->request;	
		}
		
		$view->offers = $this->offer->find($this->request);
		$suburbs = new model('suburbs');
		$view->suburbs = $suburbs->getAll();
		$nextLink = $this->getPrevNextPage('next');
		$prevLink = $this->getPrevNextPage('prev');
		$view->flash = "mamame er webo  barra roja";
		
//		print_r($view->offers);
		include('offer_index_view.php');
	}
	
	/**
	 * Function to Store a Venue Rating
	 * User, Venue_id, Rating Level have to be s
	 */
	function rate(){
		$this->requireLogin();
		$rate = new model("venue_rates");
		$this->request["user_id"]=$_SESSION['user']['id'];
		$this->request["venue_id"]=$this->request['id'];
		//print_r($rate->create($this->request)); 
		
				if($rate->create($this->request)){
			$this->setFlash("Thanks for rating!");
		}
		else {
			$this->setFlash("Upps! there was a problem with your rate, please try again.");
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
	
}
?>